package com.mes.defece.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mes.common.pojo.MESResult;
import com.mes.defece.mapper.IncomingMapper;
import com.mes.defece.service.IncomingService;
import com.mes.pojo.defece.Incoming;
import com.mes.pojo.defece.TestingProject;
import com.mes.pojo.device.Device;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * @Author 侯伟豪
 * @Company 起点编程
 * @Description
 * @Time 2024/10/22 14:40
 */
@Service
public class IncomingServiceImpl implements IncomingService {
    @Autowired
    private IncomingMapper incomingMapper;

    @Override
    public MESResult selectAll(Incoming incoming, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Incoming> incomingList = incomingMapper.selectAll(incoming);
        return MESResult.success(new PageInfo<>(incomingList));
    }

    @Override
    public MESResult addIncoming(Incoming incoming) {
        incoming.setIncomingDocumentStatus(1);
        incoming.setMaterialPerson("1");
        if (incoming.getIncomingQualifiedNumber() == null){
            incoming.setIncomingQualifiedNumber(1);
        }
        return MESResult.success(incomingMapper.insert(incoming));
    }

    @Override
    public MESResult selectById(Long incomingId) {
        return MESResult.success(incomingMapper.selectById(incomingId));
    }

    @Override
    public MESResult updateIncomingById(Incoming incoming) {
        return MESResult.success(incomingMapper.updateIncomingById(incoming));
    }

    @Override
    public MESResult getExport(Incoming incoming, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Incoming> incomingList = incomingMapper.selectAll(incoming);
        File folder = new File("D:\\MES\\Excel");
        if (!folder.exists()){
            folder.mkdirs();
        }
        EasyExcel.write("D:\\MES\\Excel\\incoming.xlsx", Incoming.class).sheet("来料检验数据" +  page + limit ).doWrite(incomingList);
        return MESResult.success(new PageInfo<>(incomingList));
    }

    @Override
    public MESResult deleteById(Long incomingId) {
        Incoming incoming = new Incoming();
        incoming.setIncomingId(incomingId);
        incoming.setIncomingDocumentStatus(99);
        return MESResult.success(incomingMapper.deleteById(incoming));
    }
}